<?php

class CategoryModel extends CI_Model {


	public function getById($id = NULL) {

		if ($id != NULL) {
			$this -> db -> where('id', $id);
			$query = $this -> db -> get('blog_category');
			return $query -> row_array();
		} 
	}

	public function getAll($per_page = NULL, $offset = NULL) {

		$query = $this -> db -> get('blog_category', $per_page, $offset);
		return $query -> result_array();

	}

	public function search($term = "") {

		if ($term != NULL && strlen($term)) {

			$array = array('name' => $term);

			$this -> db -> like($array);

			$query = $this -> db -> get('blog_category');

			return $query -> result_array();

		}
	}

	public function add($data) {

		/**
		 * clear not necessary data.
		 * */
	
		
		unset($data['submit']);
		$data['enabled']=( isset($data['enabled']) == null ) ? 0 : 1;
		
		/**
		 * Adding the date
		 * */

		$data['date_created'] = date('Y-m-d H:i:s');

		$this -> db -> insert('blog_category', $data);
		return $this -> db -> insert_id();
	}

	public function updaterecord($data, $id) {

		$id = (int)$id;
	
		$data['enabled']=( isset($data['enabled']) == null ) ? 0 : 1;

		$this -> db -> where('id', $id);
		return $this -> db -> update('blog_category', $data) ? TRUE : FALSE;

	}

	public function delete($id) {
		$this -> db -> where('id', $id);
		$this -> db -> delete('blog_category');
	}

}
